package com.act262.vote.ui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.swing.Box;
import javax.swing.ButtonModel;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JToggleButton;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.filechooser.FileFilter;
import javax.swing.plaf.FileChooserUI;

import java.awt.Dimension;

import javax.swing.BoxLayout;

import java.awt.Rectangle;
import java.awt.ComponentOrientation;

import javax.swing.border.SoftBevelBorder;
import javax.swing.border.BevelBorder;
import javax.swing.border.LineBorder;

import java.awt.Color;

import javax.swing.ButtonGroup;

import com.act262.vote.Vote;
import com.act262.vote.Vote.CallBack;
import com.act262.vote.bean.Params;
import com.act262.vote.bean.Rule;
import com.act262.vote.bean.User;
import com.act262.vote.utils.ExcelUtils;

public class MainUi extends JFrame implements CallBack {
	private JTextField tf_excelPath;
	private JTextField tf_ids;
	private JTextArea ta_viewState;
	private JTextArea ta_eventValidation;
	private JTextField tf_dataStartIndex;
	private JTextField tf_dataEndIndex;
	private JTextField tf_dataCount;
	private JTextField tf_time;
	private final ButtonGroup buttonGroup = new ButtonGroup();
	private JTextArea ta_result;
	private JButton btn_start;
	private JButton btn_pause;
	private JButton btn_stop;
	private JButton btn_clear;
	private JButton btn_save;
	private JRadioButton rbtn_10s;
	private JRadioButton rbtn_30s;
	private JRadioButton rbtn_1m;
	private JRadioButton rbtn_5m;
	private JRadioButton rbtn_10m;
	private JRadioButton rbtn_30m;
	private JRadioButton rbtn_1h;

	/** 编号规则表达式 */
	String idsRegex = "^((\\d{1,2}){1})([,](\\d{1,2}[,]){0,4}(\\d{1,2}){0,1}$)?";
	/** 存储结果 */
	StringBuffer resultBuffer = new StringBuffer();

	/** 编号 */
	List<String> ids;
	/** 导入的用户数据 */
	List<User> userList;
	/** 选中的用户信息数据文件 */
	File selectFile = null;
	/** 投票器控制对象 */
	Vote vote = new Vote(MainUi.this);

	final int DIALOG_TYPE_FILE_NOT_FOUND = 0x00;
	final int DIALOG_TYPE_FILE_ERRO = 0x01;
	final int DIALOG_TYPE_FILE_NOT_SELECT = 0x02;
	final int DIALOG_TYPE_FILE_IS_NOT_FILE = 0x03;
	final int DIALOG_TYPE_INDEX_ERRO = 0x04;
	final int DIALOG_TYPE_INDEX_FORMAT_ERRO = 0x05;
	final int DIALOG_TYPE_FILE_NOT_STANDARD = 0x06;
	private JRadioButton rbtn_random;
	private JLabel status;

	public MainUi() {
		this.setBounds(100, 100, 800, 800);
		this.setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		getContentPane().setLayout(new BorderLayout(2, 2));

		initMenuBar();
		initContent();

		// this.pack();
	}

	private void initData() {
		JPanel dataPanel = new JPanel();
		getContentPane().add(dataPanel, BorderLayout.NORTH);
		dataPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));

		tf_excelPath = new JTextField();
		dataPanel.add(tf_excelPath);
		tf_excelPath.setColumns(40);

		JButton btn_importData = new JButton("导入数据");
		btn_importData.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				openFile();
			}
		});
		dataPanel.add(btn_importData);

		JToggleButton toggleButton = new JToggleButton("控制");
		toggleButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		toggleButton.setHorizontalTextPosition(SwingConstants.RIGHT);
		toggleButton.setAlignmentX(Component.RIGHT_ALIGNMENT);
		dataPanel.add(toggleButton);
	}

	private JPanel initParamsIds() {
		JPanel idsPanel = new JPanel();
		idsPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));

		JLabel label = new JLabel("输入投票的对象编号");
		label.setHorizontalAlignment(SwingConstants.CENTER);
		idsPanel.add(label);

		tf_ids = new JTextField();
		idsPanel.add(tf_ids);
		tf_ids.setColumns(40);

		JLabel ids_tip = new JLabel("(以逗号分隔开,输入1~6个编号)");
		idsPanel.add(ids_tip);

		return idsPanel;
	}

	private JPanel initParamsViewStatus() {
		JPanel viewStatusPanel = new JPanel();
		GridBagLayout gbl_viewStatusPanel = new GridBagLayout();
		gbl_viewStatusPanel.columnWidths = new int[] { 0, 0, 0, 0, 0, 0 };
		gbl_viewStatusPanel.rowHeights = new int[] { 0, 0 };
		gbl_viewStatusPanel.columnWeights = new double[] { 0.0, 0.0, 0.0, 0.0,
				0.0, Double.MIN_VALUE };
		gbl_viewStatusPanel.rowWeights = new double[] { 1.0, Double.MIN_VALUE };
		viewStatusPanel.setLayout(gbl_viewStatusPanel);

		JPanel panel_5 = new JPanel();
		panel_5.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		FlowLayout flowLayout = (FlowLayout) panel_5.getLayout();
		flowLayout.setAlignment(FlowLayout.LEFT);
		GridBagConstraints gbc_panel_5 = new GridBagConstraints();
		gbc_panel_5.insets = new Insets(0, 0, 0, 5);
		gbc_panel_5.fill = GridBagConstraints.BOTH;
		gbc_panel_5.gridx = 0;
		gbc_panel_5.gridy = 0;
		viewStatusPanel.add(panel_5, gbc_panel_5);

		JLabel lblNewLabel = new JLabel("__VIEWSTATE        ");
		panel_5.add(lblNewLabel);

		JScrollPane scrollPane = new JScrollPane();
		panel_5.add(scrollPane);

		ta_viewState = new JTextArea();
		ta_viewState.setColumns(30);
		ta_viewState.setLineWrap(true);
		ta_viewState.setRows(10);
		scrollPane.setViewportView(ta_viewState);

		JSeparator separator = new JSeparator();
		GridBagConstraints gbc_separator = new GridBagConstraints();
		gbc_separator.insets = new Insets(0, 0, 0, 5);
		gbc_separator.gridx = 2;
		gbc_separator.gridy = 0;
		viewStatusPanel.add(separator, gbc_separator);

		JPanel panel_6 = new JPanel();
		panel_6.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		FlowLayout flowLayout_1 = (FlowLayout) panel_6.getLayout();
		flowLayout_1.setAlignment(FlowLayout.LEFT);
		GridBagConstraints gbc_panel_6 = new GridBagConstraints();
		gbc_panel_6.fill = GridBagConstraints.BOTH;
		gbc_panel_6.gridx = 4;
		gbc_panel_6.gridy = 0;
		viewStatusPanel.add(panel_6, gbc_panel_6);

		JLabel lblNewLabel_1 = new JLabel("__EVENTVALIDATION");
		panel_6.add(lblNewLabel_1);

		JScrollPane scrollPane_1 = new JScrollPane();
		panel_6.add(scrollPane_1);

		ta_eventValidation = new JTextArea();
		ta_eventValidation.setRows(10);
		ta_eventValidation.setColumns(30);
		scrollPane_1.setViewportView(ta_eventValidation);

		return viewStatusPanel;
	}

	private JPanel initParamsTime() {
		JPanel setTimePanel = new JPanel();
		FlowLayout fl_setTimePanel = (FlowLayout) setTimePanel.getLayout();
		fl_setTimePanel.setAlignment(FlowLayout.LEFT);

		Box horizontalBox_1 = Box.createHorizontalBox();
		setTimePanel.add(horizontalBox_1);

		JLabel label_2 = new JLabel("间隔时间:");
		horizontalBox_1.add(label_2);

		tf_time = new JTextField();
		horizontalBox_1.add(tf_time);
		tf_time.setColumns(10);

		JLabel lblS = new JLabel("s");
		horizontalBox_1.add(lblS);

		JSeparator separator_5 = new JSeparator();
		setTimePanel.add(separator_5);

		rbtn_10s = new JRadioButton("10s");
		buttonGroup.add(rbtn_10s);
		setTimePanel.add(rbtn_10s);
		rbtn_10s.setSelected(true);// 默认选中

		rbtn_30s = new JRadioButton("30s");
		buttonGroup.add(rbtn_30s);
		setTimePanel.add(rbtn_30s);

		rbtn_1m = new JRadioButton("1min");
		buttonGroup.add(rbtn_1m);
		setTimePanel.add(rbtn_1m);

		rbtn_5m = new JRadioButton("5min");
		buttonGroup.add(rbtn_5m);
		setTimePanel.add(rbtn_5m);

		rbtn_10m = new JRadioButton("10min");
		buttonGroup.add(rbtn_10m);
		setTimePanel.add(rbtn_10m);

		rbtn_30m = new JRadioButton("30min");
		buttonGroup.add(rbtn_30m);
		setTimePanel.add(rbtn_30m);

		rbtn_1h = new JRadioButton("1h");
		buttonGroup.add(rbtn_1h);
		setTimePanel.add(rbtn_1h);

		rbtn_random = new JRadioButton("随机");
		buttonGroup.add(rbtn_random);
		setTimePanel.add(rbtn_random);
		return setTimePanel;
	}

	private JPanel initParamsIndex() {
		JPanel setDataIndexPanel = new JPanel();
		FlowLayout fl_setDataIndexPanel = (FlowLayout) setDataIndexPanel
				.getLayout();
		fl_setDataIndexPanel.setAlignment(FlowLayout.LEFT);

		JPanel panel_18 = new JPanel();
		setDataIndexPanel.add(panel_18);

		JLabel label_8 = new JLabel("起始位置:");
		panel_18.add(label_8);

		tf_dataStartIndex = new JTextField();
		tf_dataStartIndex.setText("1");
		tf_dataStartIndex.setColumns(10);
		panel_18.add(tf_dataStartIndex);

		JSeparator separator_3 = new JSeparator();
		panel_18.add(separator_3);

		JLabel label_9 = new JLabel("结束位置:");
		panel_18.add(label_9);

		tf_dataEndIndex = new JTextField();
		tf_dataEndIndex.setEditable(false);
		tf_dataEndIndex.setColumns(10);
		panel_18.add(tf_dataEndIndex);

		JSeparator separator_4 = new JSeparator();
		panel_18.add(separator_4);

		JLabel label_10 = new JLabel("数量:");
		panel_18.add(label_10);

		tf_dataCount = new JTextField();
		tf_dataCount.setText("20");
		tf_dataCount.setColumns(10);
		panel_18.add(tf_dataCount);
		return setDataIndexPanel;
	}

	private void initResultPanel() {
		JPanel resultPanel = new JPanel();
		resultPanel.setBorder(new TitledBorder(null,
				"\u8FD0\u884C\u7ED3\u679C", TitledBorder.LEFT,
				TitledBorder.TOP, null, null));
		getContentPane().add(resultPanel, BorderLayout.SOUTH);
		resultPanel.setLayout(new BorderLayout(0, 0));

		JPanel panel_11 = new JPanel();
		resultPanel.add(panel_11, BorderLayout.NORTH);
		panel_11.setLayout(new BorderLayout(0, 0));

		JPanel panel_13 = new JPanel();
		panel_11.add(panel_13, BorderLayout.NORTH);

		JPanel panel_14 = new JPanel();
		panel_14.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		FlowLayout flowLayout_2 = (FlowLayout) panel_14.getLayout();
		flowLayout_2.setAlignment(FlowLayout.LEFT);
		panel_11.add(panel_14, BorderLayout.CENTER);

		JLabel label_3 = new JLabel("当前状态:");
		panel_14.add(label_3);

		status = new JLabel("Ready Go");
		panel_14.add(status);

		Box horizontalBox = Box.createHorizontalBox();
		horizontalBox.setBorder(new EmptyBorder(0, 100, 0, 10));
		panel_14.add(horizontalBox);

		btn_start = new JButton("开始");
		btn_start.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				start();
			}
		});
		horizontalBox.add(btn_start);

		JSeparator separator_1 = new JSeparator();
		horizontalBox.add(separator_1);

		btn_pause = new JButton("暂停");
		btn_pause.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				pause();
			}
		});
		horizontalBox.add(btn_pause);

		JSeparator separator_2 = new JSeparator();
		horizontalBox.add(separator_2);

		btn_stop = new JButton("停止");
		btn_stop.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				stop();
			}
		});
		horizontalBox.add(btn_stop);

		JPanel panel = new JPanel();
		panel.setBorder(new EmptyBorder(0, 150, 0, 0));
		panel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
		panel_14.add(panel);
		FlowLayout fl_panel = new FlowLayout(FlowLayout.RIGHT, 5, 5);
		panel.setLayout(fl_panel);

		btn_clear = new JButton("清空显示");
		btn_clear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				clear();
			}
		});
		panel.add(btn_clear);

		btn_save = new JButton("保存结果");
		btn_save.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JFileChooser chooser = new JFileChooser();
				chooser.setDialogTitle("保存结果到文件");

				// 打开窗体,阻塞其他,直到释放窗体
				chooser.showSaveDialog(MainUi.this);

				File file = chooser.getSelectedFile();
				// 防止中途退出,没有选择文件导致异常
				if (null != selectFile) {
					System.out.println("open file  " + file.getAbsolutePath());

					if (save2File(file)) {
						JOptionPane.showMessageDialog(MainUi.this, "保存文件成功");
					} else {
						JOptionPane.showMessageDialog(MainUi.this, "保存文件失败");
					}
				}
			}
		});
		panel.add(btn_save);

		JPanel panel_15 = new JPanel();
		panel_11.add(panel_15, BorderLayout.SOUTH);

		JScrollPane scrollPane_2 = new JScrollPane();
		resultPanel.add(scrollPane_2, BorderLayout.CENTER);

		ta_result = new JTextArea();
		ta_result.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
		ta_result.setRows(12);
		ta_result.setText("");
		scrollPane_2.setViewportView(ta_result);

	}

	private void initContent() {

		// 位置:上
		initData();

		// 位置:中
		JPanel paramsPanel = new JPanel();
		paramsPanel.setBorder(new TitledBorder(null,
				"\u53C2\u6570\u8BBE\u7F6E", TitledBorder.LEFT,
				TitledBorder.TOP, null, null));
		paramsPanel.setMaximumSize(new Dimension(800, 500));
		paramsPanel.setBounds(0, 0, 800, 500);
		getContentPane().add(paramsPanel, BorderLayout.CENTER);
		paramsPanel.setLayout(new BorderLayout(0, 0));

		Box verticalBox = Box.createVerticalBox();
		verticalBox.setBorder(null);
		paramsPanel.add(verticalBox, BorderLayout.NORTH);

		// 添加编号的面板
		verticalBox.add(initParamsIds());

		// 分隔
		JPanel panel_3 = new JPanel();
		verticalBox.add(panel_3);

		// 添加状态的参数
		verticalBox.add(initParamsViewStatus());

		// 分隔
		JPanel panel_8 = new JPanel();
		verticalBox.add(panel_8);

		// 添加设置时间的面板
		verticalBox.add(initParamsTime());

		// 分隔
		JPanel panel_12 = new JPanel();
		verticalBox.add(panel_12);

		// 添加设置起始位置,计数量面板
		verticalBox.add(initParamsIndex());

		JPanel panel = new JPanel();
		verticalBox.add(panel);

		// 位置:下
		initResultPanel();

	}

	private void initMenuBar() {
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);

		JMenu menu = new JMenu("系统管理");
		menuBar.add(menu);

		JMenuItem menuItem = new JMenuItem("文件");
		menu.add(menuItem);

		JMenuItem menuItem_1 = new JMenuItem("退出");
		menu.add(menuItem_1);

		JMenu menu_1 = new JMenu("系统设置");
		menuBar.add(menu_1);

		JMenuItem menuItem_2 = new JMenuItem("设置参数");
		menu_1.add(menuItem_2);
	}

	private void pause() {
		if (vote.isRunning()) {
			vote.pause();
			status.setText("pause...");
			btn_pause.setText("继续");
		} else if (vote.isPause()) {
			vote.continueRun();
			status.setText("runing...");
			btn_pause.setText("暂停");
		} else if (vote.isStop()) {
			JOptionPane.showMessageDialog(this, "还没开始运行");
		}

	}

	private void stop() {
		if (vote.isRunning() || vote.isPause()) {
			vote.stop();
			status.setText("stop...");
			btn_start.setEnabled(true);
		} else {
			JOptionPane.showMessageDialog(this, "还没开始运行");
		}
	}

	private void start() {
		// 校验输入格式
		if (verifyIds(tf_ids.getText().trim())) {
			// 读写用户数据错误
			if (!readUserData()) {
				return;
			}

			final List<Rule> ruleList = new ArrayList<>();

			Params params = null;
			for (User user : userList) {
				params = new Params();
				// 添加参赛编号
				for (int i = 1; i <= ids.size(); i++) {
					params.putParams(Params.AddVote_0 + i, ids.get(i - 1));
				}
				params.putUser(user);
				params.putParams(Params.__VIEWSTATE, ta_viewState.getText()
						.trim());
				params.putParams(Params.__EVENTVALIDATION, ta_eventValidation
						.getText().trim());

				ruleList.add(params);
				params = null;
			}
			vote.setTime(getTime());

			new Thread(new Runnable() {

				@Override
				public void run() {
					vote.start(ruleList);
				}
			}).start();

			System.out.println("start run ...");
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (vote.isRunning()) {
				btn_start.setEnabled(false);
				status.setText("running...");
			}
		}

	}

	private int getTime() {
		if (!"".equals(tf_time.getText().trim())) {
			return Integer.parseInt(tf_time.getText().trim());
		} else {
			if (rbtn_10s.isSelected()) {
				return 10;
			} else if (rbtn_30s.isSelected()) {
				return 30;
			} else if (rbtn_1m.isSelected()) {
				return 60;
			} else if (rbtn_5m.isSelected()) {
				return 300;
			} else if (rbtn_10m.isSelected()) {
				return 600;
			} else if (rbtn_30m.isSelected()) {
				return 1800;
			} else if (rbtn_1h.isSelected()) {
				return 3600;
			} else if (rbtn_random.isSelected()) {
				return (int) (Math.random() * 100);
			}
		}
		return 0;
	}

	/** 从Excel文件流中读取用户 */
	private boolean readUserData() {
		// 没有导入数据
		if ("".equals(tf_excelPath.getText().trim()) || null == selectFile) {
			showDialog(DIALOG_TYPE_FILE_NOT_SELECT);
			return false;
		}
		try {

			if (!(selectFile.isFile() && selectFile.exists() && selectFile
					.canRead())) {
				showDialog(DIALOG_TYPE_FILE_IS_NOT_FILE);
				return false;
			}

			int startIndex = Integer.parseInt(tf_dataStartIndex.getText()
					.trim());
			int count = Integer.parseInt(tf_dataCount.getText().trim());

			if (startIndex <= 0 || count <= 0) {
				showDialog(DIALOG_TYPE_INDEX_ERRO);
			}
			// 读取指定位置开始,一定数量的数据
			userList = ExcelUtils.getUserListFromExcel(selectFile, startIndex,
					count);
			if (null != userList) {
				return true;
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			showDialog(DIALOG_TYPE_FILE_NOT_FOUND);
			return false;
		} catch (IOException e) {
			e.printStackTrace();
			showDialog(DIALOG_TYPE_FILE_ERRO);
			return false;
		} catch (NumberFormatException e) {
			e.printStackTrace();
			showDialog(DIALOG_TYPE_INDEX_FORMAT_ERRO);
			return false;
		} catch (IllegalStateException e) {
			e.printStackTrace();
			showDialog(DIALOG_TYPE_FILE_NOT_STANDARD);
			return false;
		}
		return false;
	}

	private void showDialog(int type) {
		switch (type) {
		case DIALOG_TYPE_FILE_NOT_FOUND:
			JOptionPane.showMessageDialog(this,
					"不存在该文件 " + tf_excelPath.getText(), "",
					JOptionPane.WARNING_MESSAGE);
			break;

		case DIALOG_TYPE_FILE_ERRO:
			JOptionPane.showMessageDialog(this, tf_excelPath.getText()
					+ " 读写错误", "", JOptionPane.WARNING_MESSAGE);
			break;

		case DIALOG_TYPE_FILE_NOT_SELECT:
			int r = JOptionPane.showOptionDialog(MainUi.this, "还没有导入数据", "警告",
					JOptionPane.WARNING_MESSAGE, JOptionPane.WARNING_MESSAGE,
					null, new Object[] { "导入数据", "取消" }, "0"); // 默认点击导入数据

			System.out.println(r);
			if (r == 0) {
				openFile();
			}
			break;

		case DIALOG_TYPE_FILE_IS_NOT_FILE:
			JOptionPane.showMessageDialog(this, tf_excelPath.getText()
					+ " 不是一个可读文件或者读写权限不够", "", JOptionPane.WARNING_MESSAGE);
			break;

		case DIALOG_TYPE_INDEX_FORMAT_ERRO:
			JOptionPane.showMessageDialog(this, "输入的起始位置或者数量格式不对", "警告",
					JOptionPane.WARNING_MESSAGE);
			break;

		case DIALOG_TYPE_INDEX_ERRO:
			JOptionPane.showMessageDialog(this, "输入的起始位置或者数量应该大于0", "警告",
					JOptionPane.WARNING_MESSAGE);
			break;

		case DIALOG_TYPE_FILE_NOT_STANDARD:
			JOptionPane.showMessageDialog(this, "用户数据表不标准", "读写错误",
					JOptionPane.WARNING_MESSAGE);
			break;
		default:
			break;
		}

	}

	/**
	 * 打开窗体选择文件
	 */
	private void openFile() {
		JFileChooser chooser = new JFileChooser();
		chooser.setDialogTitle("打开Excel文件");
		chooser.setFileFilter(new ExcelFileFilter());

		chooser.showOpenDialog(MainUi.this);// 打开窗体,阻塞其他,直到释放窗体

		selectFile = chooser.getSelectedFile();
		// 防止中途退出,没有选择文件导致异常
		if (null != selectFile) {
			System.out.println("open file  "
					+ chooser.getSelectedFile().getAbsolutePath());

			tf_excelPath.setText(chooser.getSelectedFile().getAbsolutePath());
		}
	}

	/**
	 * 校验输入的编号是否符合规则
	 * 
	 * @param str
	 * @return
	 */
	private boolean verifyIds(String str) {
		if (str.equals("")) {
			JOptionPane.showMessageDialog(this, "请输入投票的参赛编号");
			return false;
		}
		try {
			if (str.matches(idsRegex)) {
				ids = Arrays.asList(str.split(","));
				return true;
			} else {
				JOptionPane.showMessageDialog(this, "输入格式不对,重新输入");
			}
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, "出现严重的问题了,请查看哪里出错了!", "警告",
					JOptionPane.WARNING_MESSAGE);
			e.printStackTrace();
			return false;
		}
		return false;
	}

	/** 文件类型过滤器 */
	private final class ExcelFileFilter extends FileFilter {

		@Override
		public boolean accept(File f) {
			// 过滤指定的文件类型
			if (f.isDirectory() || f.getName().endsWith(".xls")
					|| f.getName().endsWith(".xlsx")) {
				return true;
			}
			return false;
		}

		@Override
		public String getDescription() {
			return ".xls .xlsx";
		}

	}

	@Override
	public void updateResult(String result) {
		resultBuffer.append(result + "\n");

		// 更新显示结果
		ta_result.setText(resultBuffer.toString());
		System.out.println("update : " + resultBuffer.toString());
	}

	/** 清空显示结果 */
	private void clear() {
		resultBuffer.delete(0, resultBuffer.length());
		ta_result.setText("");
		// ta_result.validate();
	}

	/** 保存到文件 */
	private boolean save2File(File file) {
		FileOutputStream out = null;
		try {
			out = new FileOutputStream(file);
			out.write(resultBuffer.toString().getBytes());
			out.flush();
			out.close();
			return true;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}

}
